package com.huahang.fashionpress.controller;

import com.huahang.fashionpress.form.RegistFormUser;
import com.huahang.fashionpress.service.impl.RegistServiceImpl;
import com.huahang.fashionpress.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 注册控制
 * @author : 高晓曦
 * @since : 2019/08/22
 * @version : 1.0
 */
@Controller
@RequestMapping("/register")
public class RegistController {
    @Autowired
    private RegistServiceImpl registService;

    //跳转注册页面
    @RequestMapping("/")
    public String getRegisterView(){
        return "client/register";
    }

    //注册用户名检查
    @RequestMapping(value = "/registerNameCheck",method = RequestMethod.POST, headers = "Accept=application/json", produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String registerNameCheck(@RequestBody String username){
        List strArray = Arrays.asList(username.split("\""));
        String formname = (String) strArray.get(3);
        String data = null;
        if(!registService.userAlreadyExsist(formname))
        {
            //用户名已存在
            data = "yes";
        }


        else
        {

            data = "no";
        }
        return data;
    }
    //注册邮箱检查
    @RequestMapping(value = "/registerEmailCheck",method = RequestMethod.POST, headers = "Accept=application/json", produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String registerEmailCheck(@RequestBody String email){
        List strArray = Arrays.asList(email.split("\""));
        String formemail = (String) strArray.get(3);
        String data = null;
        if(!registService.emailAlreadyExsist(formemail))
        {
            data = "yes";
        }
        else
        {
            data = "no";
        }
        return data;
    }
    //注册电话检查
    @RequestMapping(value = "/registerPhoneCheck",method = RequestMethod.POST, headers = "Accept=application/json", produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String registerPhoneCheck(@RequestBody String phone){
        List strArray = Arrays.asList(phone.split("\""));
        String formphone = (String) strArray.get(3);
        String data = null;
        if(!registService.phoneAlreadyExsist(formphone))
        {
            data = "yes";
        }
        else
        {
            data = "no";
        }
        return data;
    }
    //注册检查
    @RequestMapping(value = "/registerCheck")
    public String registerCheck(String username,String password,String email,String phone){
        //将用户名和密码封装
        RegistFormUser user = new RegistFormUser();
        user.setUserUsername(username);
        user.setUserPassword(Md5Util.getMd5String(password));
        user.setUserEmail(email);
        user.setUserPhone(phone);
        if(registService.insertNewUser(user))
        {
            return "client/login";
        }
        else
        {
            return "client/register";
        }

    }
}
